package com.example.houqin_oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.comment_oa.pojos.PersonalEffects;
import com.example.comment_oa.pojos.RepairRecord;
import com.example.comment_oa.pojos.PublicWare;
import com.example.comment_oa.pojos.ReplaceRecord;
import com.example.houqin_oa.mapper.PersonalEffectsMapper;
import com.example.houqin_oa.mapper.PublicWareMapper;
import com.example.houqin_oa.mapper.ReplaceRecordMapper;
import com.example.houqin_oa.service.RepairRecordService;
import com.example.houqin_oa.mapper.RepairRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author liang
* @description 针对表【repair_record】的数据库操作Service实现
* @createDate 2022-10-12 20:13:28
*/
@Service
public class RepairRecordServiceImpl extends ServiceImpl<RepairRecordMapper, RepairRecord>
    implements RepairRecordService{

    //维修
    @Autowired
    RepairRecordMapper repairRecordMapper;
    //更换
    @Autowired
    ReplaceRecordMapper replaceRecordMapper;
    //个人物品
    @Autowired
    PersonalEffectsMapper personalEffectsMapper;
    //公共物品
    @Autowired
    PublicWareMapper publicWareMapper;




    @Override
    public List<RepairRecord> RepairApply(RepairRecord repairRecord) {
        int state=0;
        return repairRecordMapper.RepairApply(repairRecord,state);
    }

    @Override
    public boolean Operation(RepairRecord repairRecord) {
        boolean AddReplace = false;
        boolean upEffect = true;
        String str = repairRecord.getRepairGoodsNo();
        System.err.println(repairRecord);
        if(repairRecord.getRepairState().equals("2")){
            repairRecord.setRepairState("2");
            //修改维修状态
            AddReplace = repairRecordMapper.Operation(repairRecord);
            if(str.substring(0,2).equals("LQ")){
                personalEffectsMapper.UpSet(repairRecord.getRepairGoodsNo(),repairRecord.getRepairState());
            }else if (str.substring(0,2).equals("GG")){
                publicWareMapper.UpSet(repairRecord.getRepairGoodsNo(),repairRecord.getRepairState());
            }

           return Scrap(repairRecord);
        }else if(str.substring(0,2).equals("LQ")){
            //修改个人物品状态
            upEffect = personalEffectsMapper.upEffectsState(repairRecord);
            if(repairRecord.getRepairState().equals("3")){
                repairRecord.setRepairState("1");
            }
            //修改维修状态
            AddReplace = repairRecordMapper.Operation(repairRecord);
        }else if(str.substring(0,2).equals("GG")){
            //修改记录状态
            if(repairRecord.getRepairState().equals("1")){
                repairRecord.setRepairState("10");
            }
            AddReplace = repairRecordMapper.Operation(repairRecord);
            //修改公共物品状态
            if(repairRecord.getRepairState().equals("1") || repairRecord.getRepairState().equals("10") || repairRecord.getRepairState().equals("3")){
                repairRecord.setRepairState("0");
            }
            upEffect = publicWareMapper.UpRear(repairRecord);
        }
        if(!AddReplace || !upEffect){return false;}else {return true;}
    }

    public boolean Scrap(RepairRecord r){
        String str = r.getRepairGoodsNo();
        ReplaceRecord replaceRecord = new ReplaceRecord();

        r.setRepairNoter(r.getRepairPeople()+"申请的报废");

        r.setRepairState("0");
        if(str.substring(0,2).equals("LQ")){
            PersonalEffects p = personalEffectsMapper.selectNo(r.getRepairGoodsNo());
            replaceRecord = new ReplaceRecord(null,r.getRepairGoodsNo(),r.getRepairGoodsName(),null,null,new Date(),null, r.getRepairApplyPeople(), r.getRepairContact(), r.getRepairPeople(),null,p.getPersonalSum(), p.getPersonalAmount(),null,0,r.getRepairNoter(),null,"1",r.getRepairState());;
        }else if(str.substring(0,2).equals("GG")){
            PublicWare w = publicWareMapper.SelectNo(r.getRepairGoodsNo());
            replaceRecord = new ReplaceRecord(null,r.getRepairGoodsNo(),r.getRepairGoodsName(),null,null,new Date(),null, r.getRepairApplyPeople(), r.getRepairContact(), r.getRepairPeople(),null,w.getPublicSum(),w.getPublicAmount(),null,0,r.getRepairNoter(),null,"1",r.getRepairState());
        }

        int PD = replaceRecordMapper.insert(replaceRecord);

        if(PD == 0){
            System.err.println("1");
            return false;

        }else {
            System.err.println("2");
            return true;
        }

    }



    @Override
    public List<RepairRecord> KeepAsk(RepairRecord repairRecord) {
        int State = 0 ;
        return repairRecordMapper.KeepAsk(repairRecord,State);
    }

    @Override
    public boolean NewMaintenance(RepairRecord repairRecord) {
        repairRecord.setRepairState("0");
        List<RepairRecord>  RepairNo= repairRecordMapper.repairNo(repairRecord);
        if(RepairNo.size()==0){
            repairRecord.setRepairState("10");
            personalEffectsMapper.upEffectsState(repairRecord);
            repairRecord.setRepairState("0");
            return repairRecordMapper.AddRear(repairRecord);
        }else {
            return false;
        }
    }

    @Override
    public Boolean AddRear(RepairRecord repairRecord) {
        boolean AddR ;
        boolean UPRear ;
        repairRecord.setRepairState("0");
        List<RepairRecord>  RepairNo= repairRecordMapper.repairNo(repairRecord);
        if(RepairNo.size()==0){
            AddR = repairRecordMapper.AddRear(repairRecord);
            repairRecord.setRepairState("1");
            UPRear = publicWareMapper.UpRear(repairRecord);

            if(AddR && UPRear){
                return true;
            }else{
                return false;
            }

        }else {
            return false;
        }
    }

    @Override
    public List<RepairRecord> conAllRepair() {
        return replaceRecordMapper.conAllRepair("0","0");
    }

    @Override
    public Boolean upRepair(RepairRecord repairRecord) {
        return replaceRecordMapper.upRepair(repairRecord);
    }
}




